<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-form :inline="true" :model="queryinfo" class="demo-form-inline" size="mini">
        <el-form-item >
          <el-input v-model="queryinfo.title" placeholder="活动名称" clearable/>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryinfo.statu" clearable filterable>
            <el-option label="生成" value="0"></el-option>
            <el-option label="进行中" value="1"></el-option>
            <el-option label="下线" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="GetData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="DataOpen(1)">添加</el-button>
        </el-form-item>
      </el-form>
      <hr>
      <!--数据-->
      <el-table :data="tableData" style="width: 100%" size="mini">
        <el-table-column prop="id" label="活动ID" />
        <el-table-column prop="title" label="公司名字" />
        <el-table-column prop="template_name" label="活动类型" />
        <el-table-column prop="activity_name" label="活动名称" />
        <el-table-column prop="activity_imgs" label="活动图片" />
        <el-table-column prop="activity_statu" label="活动状态"  >
          <template #default="props">
            <el-tag size="mini" v-if="props.row.activity_statu === 0" type="success">生成</el-tag>
            <el-tag size="mini" v-else-if="props.row.activity_statu === 1" type="success">进行中</el-tag>
            <el-tag size="mini" v-else type="warning">结束</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="activity_start" label="活动开始时间" width="180">
          <template #default="props">{{ formatDate(props.row.activity_start) }}</template>
        </el-table-column>
        <el-table-column prop="activity_end" label="活动结束时间" width="180">
          <template #default="props">{{ formatDate(props.row.activity_end) }}</template>
        </el-table-column>
        <el-table-column prop="addtime" label="添加时间" width="180">
          <template #default="props">{{ formatDate(props.row.addtime) }}</template>
        </el-table-column>
        <el-table-column prop="uptime" label="更新时间" width="180">
          <template #default="props">{{ formatDate(props.row.uptime) }}</template>
        </el-table-column>


        <el-table-column prop="activity_path" label="配置地址"  />
        <el-table-column  label="##" width="180" >
          <template #default="props">
            <el-button size="mini" @click="DataOpen(props.row)">Edit</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
          background
          :current-page="queryinfo.page"
          :page-sizes="[3,10, 50, 100, 1000]"
          :page-size="queryinfo.size"
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="changePage"
      />
    </el-card>

    <!--添加-->
    <el-drawer
        v-model="DataDrawer"
        :title="DataType===1?'添加':'更新'"
        direction="rtl"
        :before-close="DataClose"
    >
      <el-form  :model="DataForm" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="DataForm.activity_name" ></el-input>
        </el-form-item>
        <el-form-item label="活动公司">
          <el-select v-model="DataForm.comid" placeholder="所属公司" clearable filterable>
            <el-option
                v-for="item in company"
                :key="item.comid"
                :label="item.title"
                :value="item.comid"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动类型">
          <el-select v-model="DataForm.activity_template_id" placeholder="活动类型" clearable filterable>
            <el-option
                v-for="item in template"
                :key="item.template_id"
                :label="item.template_name"
                :value="item.template_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动图片">
          <el-input v-model="DataForm.activity_imgs" ></el-input>
        </el-form-item>
        <el-form-item label="模板配置">
          <el-input v-model="DataForm.activity_path" ></el-input>
        </el-form-item>
        <el-form-item label="活动时间">
          <el-date-picker
              v-model="DataForm.activity_start"
              type="datetime"
              placeholder="活动开始时间"
          />
          <el-date-picker
              v-model="DataForm.activity_end"
              type="datetime"
              placeholder="活动结束时间"
          />
        </el-form-item>
        <el-form-item label="活动状态" >
          <el-radio v-model="DataForm.activity_statu" :label='1'>启用</el-radio>
          <el-radio v-model="DataForm.activity_statu" :label='2'>结束</el-radio>
        </el-form-item>
        <el-form-item label="活动说明">
          <el-input
              v-model="DataForm.activity_content"
              :rows="2"
              type="textarea"
              placeholder="Please input"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="DataSubmit">{{ DataType === 1? "Create" : "Updata" }}</el-button>
          <el-button @click="DataClose">Clone</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

  </div>
</template>

<script>
import {onMounted, reactive, toRefs} from "vue";
import {ElMessage} from "element-plus";
import axios from "@/utils/axios";
import {formatDate} from '@/utils/date'
export default {
  name: "user",
  setup() {
    const state = reactive({
      queryinfo: {
        page: 1,
        size: 10
      },
      currentPage: 1, // 当前页
      pageSize: 10, // 分页大小
      total: 0,
      tableData: [],


      //更新添加
      DataDrawer: false,
      DataForm:{},
      DataType:1, // 1添加 2 更新

      //公司
      company:[],
      template:[]
    })
    onMounted(() => {
      GetData();GetCom();GetTemp()
    })



    const GetCom = async () => {
      const {data: res} = await axios.get('activity/com', {params: state.queryinfo})
      if (res.code !== 1000) return ElMessage.error(res.msg)
      state.company = res.data
    }
    const GetTemp = async () => {
      const {data: res} = await axios.get('activity/temp', {params: state.queryinfo})
      if (res.code !== 1000) return ElMessage.error(res.msg)
      state.template = res.data
    }

    //抽屉操作
    const DataOpen = (data) => {
      state.DataDrawer = true
      if (data === 1){
        state.DataType = 1
      }else{
        state.DataType = 2
        state.DataForm = data
      }
    }
    const DataClose = () => {
      state.DataDrawer = false
      state.DataForm = {}
    }
    const DataSubmit = async () => {
      if (state.DataType === 1){
        const {data: res} = await axios.put('activity', state.DataForm)
        if (res.code !== 1000) return ElMessage.error(res.msg)
      }else{
        const {data: res} = await axios.post('activity', state.DataForm)
        if (res.code !== 1000) return ElMessage.error(res.msg)
      }
      ElMessage.success("成功")
      GetData()
      DataClose()
    }


    // 获取列表
    const GetData = async () => {
      const {data: res} = await axios.get('activity', {params: state.queryinfo})
      if (res.code !== 1000) return ElMessage.error(res.msg)
      state.tableData = res.data.list
      state.total = res.data.total
    }
    const changePage = (val) => {
      state.queryinfo.page = val
      GetData()
    }
    const handleSizeChange = (val) => {
      state.queryinfo.size = val
      GetData()
    }

    return {
      ...toRefs(state),
      GetData,
      changePage,
      handleSizeChange,
      DataOpen,
      DataClose,
      DataSubmit,
      formatDate,
      GetCom,
      GetTemp

    }
  }
}
</script>

<style scoped>

</style>